package com.shop.shoporder.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

/*图片合成类*/
public class MergeImage {



        public static String  mergeImage(String bigPath, String smallPath, int x, int y ,String localPath) throws IOException {
            try {
                BufferedImage small;
                BufferedImage big = ImageIO.read(new File(bigPath));
                if (smallPath.contains("http")) {

                    URL url = new URL(smallPath);
                    small = ImageIO.read(url);
                } else {
                    small = ImageIO.read(new File(smallPath));
                }

                Graphics2D g = big.createGraphics();
//                float fx = Float.parseFloat(x);
//                float fy = Float.parseFloat(y);
//                int x_i = (int) fx;
//                int y_i = (int) fy;

                g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
                g.dispose();

                File path = new File(localPath);//服务器
                if (!path.exists()) {
                    path = new File("");
                }
                String uuId = UUID.randomUUID().toString().replace("-", "").substring(5, 10);
                String   tarImg = path + "/" + "ewmbg-" + uuId + ".png";//绝对路径

                System.out.println("tarImg:"+tarImg);

                ImageIO.write(big, "png", new File(tarImg));
                return  tarImg;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }


    /*
     * 图片按比率缩放
     * size为文件大小
     */
    public static void zoomImage(String src,String dest,Integer size) throws Exception {
        Logger logger=LoggerFactory.getLogger(MergeImage.class);
        File srcFile = new File(src);
        File destFile = new File(dest);

        long fileSize = srcFile.length();

        if(fileSize < size * 1024)   //文件大于size k时，才进行缩放,注意：size以K为单位
            return;

        Double rate = (size * 1024 * 0.5) / fileSize; // 获取长宽缩放比例

        BufferedImage bufImg = ImageIO.read(srcFile);
        Image Itemp = bufImg.getScaledInstance(bufImg.getWidth(), bufImg.getHeight(), bufImg.SCALE_SMOOTH);

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(rate, rate), null);
        Itemp = ato.filter(bufImg, null);
        try {
            ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /*
     * 图片缩放,w，h为缩放的目标宽度和高度
     * src为源文件目录，dest为缩放后保存目录
     */
    public static void zoomImage(String src,String dest,int w,int h) throws Exception {

        double wr=0,hr=0;
        File srcFile = new File(src);
        File destFile = new File(dest);

        BufferedImage bufImg = ImageIO.read(srcFile); //读取图片
        Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);//设置缩放目标图片模板

        wr=w*1.0/bufImg.getWidth();     //获取缩放比例
        hr=h*1.0 / bufImg.getHeight();

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        Itemp = ato.filter(bufImg, null);
        try {
            ImageIO.write((BufferedImage) Itemp,dest.substring(dest.lastIndexOf(".")+1), destFile); //写入缩减后的图片
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
